﻿namespace PrismEx.Modules.DialogService
{
    using System.ComponentModel.Composition;

    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Prism.MefExtensions.Modularity;
    using Microsoft.Practices.Prism.Modularity;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.ServiceLocation;

    using PrismEx.Events;

    [ModuleExport(typeof(DialogServiceModule))]
    public class DialogServiceModule : IModule
    {
        private readonly IEventAggregator eventAggregator;

        private readonly IServiceLocator serviceLocator;

        private readonly IRegionManager regionManager;

        [ImportingConstructor]
        public DialogServiceModule(IEventAggregator eventAggregator, IServiceLocator serviceLocator)
        {
            this.eventAggregator = eventAggregator;
            this.serviceLocator = serviceLocator;
        }

        public void Initialize()
        {
            eventAggregator.Subscribe<ShowDialogEvent>(
                e =>
                    { 
                        var dialog = serviceLocator.GetInstance<Dialog>();
                        dialog.ShowDialog(e.Uri);
                    });
        }
    }
}